package edu.zju.udms.dnavigator.core.server.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.TermQuery;

import edu.zju.udms.dnavigator.core.entity.Relation;
import net.sf.json.JSONObject;

public class EntityGlobalLineageServlet extends NavigatorServlet{
	private static final long serialVersionUID = 3808476541879174800L;

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		MatchAllDocsQuery matchAll = new MatchAllDocsQuery();
		BooleanQuery boolQuery = new BooleanQuery();
		boolQuery.add(new TermQuery(new Term("type","Column")), BooleanClause.Occur.MUST);
		/*
		boolQuery.add(new TermQuery(new Term("type","Table")), BooleanClause.Occur.SHOULD);
		boolQuery.add(new TermQuery(new Term("type","Database")), BooleanClause.Occur.SHOULD);
		boolQuery.add(new TermQuery(new Term("type","Job")), BooleanClause.Occur.SHOULD);
		boolQuery.add(new TermQuery(new Term("type","Step")), BooleanClause.Occur.SHOULD);
		*/
		List<Map<String, String>> entities = repository.searchMap(matchAll, 10000);
		List<Relation> relations = repository.searchRelations(matchAll, 10000);
		JSONObject res = new JSONObject();
		res.put("relations", relations);
		res.put("entities", entities);
		resp.setCharacterEncoding("UTF-8");
		PrintWriter writer = resp.getWriter();
		writer.write(res.toString());
	}
}
